package cuiying.rbac.service.array;

import cuiying.rbac.domain.dto.UserDto;
import cuiying.rbac.domain.entity.Dept;
import cuiying.rbac.domain.vo.DeptVo;
import cuiying.rbac.domain.vo.Result;
import cuiying.rbac.domain.dto.DeptDto;
import cuiying.rbac.service.IDeptService;
import cuiying.rbac.service.IUserService;

import java.util.Arrays;

/**
 * 业务层-部门
 *
 * @author 崔颖
 * @since 2025/4/20 15:33
 */
public abstract class DeptService extends BaseService<Dept,DeptDto,DeptVo>
        implements IDeptService {


    private IUserService userService;

    private static IDeptService singleton;

    private DeptService(){
        data = new Dept[DATA_SIZE + 1];
        temp = new DeptVo[ DATA_SIZE];

    }
    public static IDeptService getInstance(){
        if(singleton == null){
            singleton = new DeptService();
        }
        return singleton;
    }


    @Override
    DeptVo convertToVo(Dept dept){
        return new DeptVo(dept.getId(),dept.name,dept.phone);
    }

    @Override
    Dept createEntity(DeptDto dto, int id){
        return new Dept(id,dto.name,dto.phone);
    }

    @Override
    public void updataEntity(DeptDto dto,Dept dept){
        dept.name = dto.name;
        dept.phone = dto.phone;
    }

    @Override
    public Result delete(int id){
        if(!existsById(id)){
            return Result.failure("删除失败，不存在该数据");
        }
        if(userService == null){
            userService = UserService.getInstance();
        }
        if(userService.deptInUse(id)){
            return Result.failure("删除失败，还有用户属于该部门，不能删除");
        }
        data[id] = null;
        return Result.success("删除成功",null);
    }

    public Result updata(UserDto dto, int id) {
        return null;
    }
}

